package com.shujia.MapReduce;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;
import java.util.*;

public class Demo12Question3 {
    public static class MyMapper extends Mapper<LongWritable, Text, Text, Text> {

        @Override
        protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, Text>.Context context) throws IOException, InterruptedException {
            // 18620192711,15733218050,1506628174,1506628265,650000,810000
            String[] splits = value.toString().split(",");
            if (splits.length == 6) {
                String receiver = splits[1];
                String receiver_cid = splits[5];

                context.write(new Text(receiver_cid), new Text(receiver));

            }


        }
    }


    public static class MyReducer extends Reducer<Text, Text, Text, IntWritable> {
        @Override
        protected void reduce(Text key, Iterable<Text> values, Reducer<Text, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {
            String provinceId = key.toString();
            // 定义一个Hashtable： key是电话号码，value是该电话被呼叫的次数
            Hashtable<String, Integer> receiver_cnt = new Hashtable<>();

            // 遍历values 构造Hashtable
            for (Text value : values) {
                String receiver = value.toString();
                Integer pre_cnt = receiver_cnt.getOrDefault(receiver, 0);
                pre_cnt++;
                receiver_cnt.put(receiver, pre_cnt);
            }
            // 做排序
            List<Map.Entry<String, Integer>> list = new ArrayList<>(receiver_cnt.entrySet());
            Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
                @Override
                public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                    return o2.getValue().compareTo(o1.getValue());
                }
            });
            // 取前三条进行输出
            int cnt = 1;
            for (Map.Entry<String, Integer> mapping : list) {
                if (cnt <= 3) {
                    String receiver = mapping.getKey();
                    Integer r_cnt = mapping.getValue();
                    context.write(new Text(provinceId + "," + receiver), new IntWritable(r_cnt));
                }
                cnt++;
            }

        }
    }

    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);
        job.setJobName("Demo12Question3");
        job.setJarByClass(Demo12Question3.class);

        // 配置Map任务
        job.setMapperClass(MyMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);

        job.setReducerClass(MyReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);


        // 配置输入输出路径
        FileInputFormat.addInputPath(job, new Path("/ques1/data1/"));
        // 判断输出路径是否存在，存在即删除
        FileSystem fs = FileSystem.get(job.getConfiguration());
        if (fs.exists(new Path("/ques3/res"))) {
            fs.delete(new Path("/ques3/res"), true);
        }
        FileOutputFormat.setOutputPath(job, new Path("/ques3/res"));

        job.waitForCompletion(true);
    }
}
